package com.hyts.nio;


import com.hyts.network.core.Server;
import com.hyts.network.service.nio.NioServer;
import com.hyts.network.task.impl.HandlerTaskExecutor;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * project-name:coffice-ext-network
 * package-name:nio
 * author:Libo/Alex
 * create-date:2018-11-24 10:45
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于:
 */
public class NioTestServer extends HandlerTaskExecutor {

    static Server connector = null;


    public static void main(String[] args)
    {
        try {
            ExecutorService excutor = Executors.newFixedThreadPool(4);
            excutor.execute(() -> {
                try {
                    start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            excutor.execute(() -> {
                try {
                    do {
                        if(connector.getInjectResultList()) {
                            System.out.println(connector.resultList);
                        }else{
                            Thread.sleep(1500);
                            System.out.println("正在监控结果列表中......."+System.currentTimeMillis());
                        }
                    } while (!connector.isClosed());
                    System.out.println("监控结束");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
            excutor.execute(() -> {
                try {
                    Thread.sleep(40000);
                    connector.stop();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
            excutor.shutdown();
        }finally {

        }
    }

    public static void start() throws IOException {
        connector = new NioServer(8089);
        connector.start(new NioTestServer());
    }


    @Override
    public byte[] deal(byte[] result) {
    	
        return (new String(result)+" ==="+(new String("121231231231231231"))).getBytes();
    }
}
